import numpy as  np
import torch
import torchvision
import os
import cv2



def img_div(img, flag):
    row, col = img.shape
    lc = np.zeros((row, col))
    data = []
    label = []
    lc = []
    # row,col获取图像的行和列；lc先定义一个和图像尺寸相同的0矩阵；data ，label 为空列表
    for r in range(1, row - 2):
        for c in range(1, col - 2):
            v1 = int(img[r - 1][c])
            v2 = int(img[r][c - 1])
            v3 = int(img[r + 1][c])
            v4 = int(img[r][c + 1])  # 像素上下左右四个像素
            x = int(img[r][c]);
            w1 = int(img[r - 1][c + 2]);
            w2 = int(img[r][c + 2]);
            w3 = int(img[r + 1][c - 1])
            w4 = int(img[r + 1][c + 1]);
            w5 = int(img[r + 1][c + 2]);
            w6 = int(img[r + 2][c - 1]);
            w7 = int(img[r + 2][c]);
            w8 = int(img[r + 2][c + 1]);
            wL = int(img[r + 2][c + 2])
            # w1到wL为像素周围的九个圈的像素
            f0 = abs(v2 - w3);
            f1 = abs(w3 - w6);
            f2 = abs(v3 - w7);
            f3 = abs(v4 - w4);
            f4 = abs(w4 - w8);
            f5 = abs(w1 - w2);
            f6 = abs(w2 - w5);
            f7 = abs(w5 - wL)
            f8 = abs(v4 - w2);
            fL = abs(w3 - v3);
            f_0 = abs(v3 - w4);
            f_1 = abs(w4 - w5);
            f_2 = abs(w6 - w7);
            f_3 = abs(w7 - w8);
            f_4 = abs(w8 - wL)
            D = f0 + f1 + f2 + f3 + f4 + f5 + f6 + f7 + f8 + fL + f_0 + f_1 + f_2 + f_3 + f_4  # D为论文中像素复杂度c
            # D1=f0+fL+f3+f_0
            '''s = np.mean([f0,f1,f2,f3,f4,f5,f6,f7,f8,fL,f_0,f_1,f_2,f_3,f_4])
            s1 = math.pow((f0-s),2);s2 = math.pow((f1-s),2);s3 = math.pow((f2-s),2);s4 = math.pow((f3-s),2);s5 = math.pow((f4-s),2)
            s6 = math.pow((f5 - s), 2);s7 = math.pow((f6-s),2);s8 = math.pow((f7-s),2);s9 = math.pow((f8-s),2);s10 = math.pow((fL-s),2)
            s11 = math.pow((f_0 - s), 2);s12 = math.pow((f_1-s),2);s13 = math.pow((f_2-s),2);s14 = math.pow((f_3-s),2);s15 = math.pow((f_4-s),2)
            ave_s = np.mean([s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13, s14, s15])#复杂度之一
            '''
            '''t = np.mean([v1,v2,v3,v4])
            t1 = math.pow((v1-t),2);t2 = math.pow((v2-t),2);t3 = math.pow((v3-t),2);t4 = math.pow((v4-t),2);
            ave_t = np.mean([t1, t2, t3, t4])  # 复杂度之一,聚类特征？
            '''
            # av = np.mean([v1, v2, v3, v4])
            # pe = abs(x-av)

            datas0 = np.array(
                [v2, v3, v4, w1, w2, w3, w4, w5, w6, w7, w8, wL, D])  # 像素周围的12个像素存到datas0,D是复杂度，先添加进去，后续删除
            x0 = np.mean([v2, v3, v4, w1, w2, w3, w4, w5, w6, w7, w8, wL])  # 12像素的平均值，x0为论文中的m
            if np.mod(r + c, 2) == 0 and flag == 0:  # 偶数点
                label.append([D, x0]);
                data.append(datas0)  # 此时的label存放的就是聚类的特征向量；data存储的为像素周围的12个像素
            if np.mod(r + c, 2) == 1 and flag == 1:  # 奇数点
                label.append([D, x0]);
                data.append(datas0)

    # data=np.array(data);data=data.astype('float32')/255.;label=np.array(label);label=label.astype('float32')/255  #数据转换成np数组归一化，提高计算效率
    return data, label


def compute_fu(img, label):
    row, col = img.shape
    # print('row', row)
    label1 = []
    data1 = []
    for r in range(0, row):
        # print(test_data[r][0][2])
        if img[r][12] < 350:  # 小于复杂度阈值独立摘出来 第13列是D
            a = img[r].tolist()  # 把满足小于350阈值的行以列表的形式赋值给a
            data1.append(a)  # 存放到data1
            # print('data2 ', test_data)
        if label[r][0] < 350:  # 把对应的特征向量独立摘出来，即（背景像素平均值和复杂度D）
            b = label[r].tolist()  # 把满足小于350阈值的行以列表的形式赋值给b
            label1.append(b)
    # print('data1', data1)
    print('len(data1)', len(data1))
    for i in range(0, len(data1)):
        data1[i].pop(12)  # 删除第13列的D，恢复12列训练像素
    # for i in range(0, len(label1)):
    # label1[i].pop(0)
    # print('data1', data1)
    data1 = np.array(data1)
    label1 = np.array(label1)
    print('data1.shape', data1.shape)
    print('data1', data1)

    # print(type(data1))
    # print(type(label1))
    print('label1.shape', label1.shape)
    print('label1', label1)

    data1 = np.array(data1);
    data1 = data1.astype('float32') / 255.;
    label1 = np.array(label1);
    label1 = label1.astype('float32') / 255  # 数据转换成np数组归一化，提高计算效率
    # test_data=np.vstack((img1_0,img1_1,img0_0,img0_1,img2_0,img2_1))
    # test_label=np.vstack((img10,img11,img00,img01,img20,img21))
    return data1, label1


def read_data(root_path):
    img_path = os.listdir(root_path)
    print(img_path)
    list1 = np.array([])
    list2 = np.array([])
    for data in img_path:
        path  = os.path.join(root_path,data)
        img =cv2.imread(path,0)
        # print(img)
        img1,label1 =img_div(img,0)
        img2,label2 = img_div(img,1)
        list1 = np.append(list1,[img1,img2])
        list2 = np.append(list2,[label1,label2])
    # print(list2)
    list1 = list1.flatten()
    list2 = list2.flatten()
    data = np.vstack(list1)
    label = np.vstack(list2)
    data,label = compute_fu(data,label)
    print(data.shape)
    return data,label



read_data('test')